﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Tanmia.Basis;
using System.Windows.Input;
using Microsoft.Practices.Prism.Commands;
using Tanmia.Basis.ViewInterface;
using Tanmia.Basis.Prism;
using Tanmia.Basis.ViewModel;
using System.Windows;
using System.Windows.Media;
using System.Windows.Controls;

namespace SaryaAccounting.UserSelection
{
    public class LoginUserSelectorViewModel : IUserSelector
    {
        public ViewInfo ViewInfo { get; protected set; }
        public ApplicationUserInfo Entity { get; set; }

        public ICommand OkCommand { get; private set; }
        public ICommand CancelCommand { get; private set; }

        public LoginUserSelectorViewModel()
        {
            ViewInfo = new Tanmia.Basis.ViewModel.ViewInfo("تسجيل الدخول", "يجب تحديد المستخدم قبل الدخول إلى البرنامج وذلك لتحديد الصلاحيات التي يمكنه القيام بها.")
            {
                IsOperationEnabled = false,
                CloseVisibility = Visibility.Hidden,
                MaximizeVisibility = Visibility.Hidden,
                MinimizeVisibility = Visibility.Hidden
            };

            OkCommand = new DelegateCommand(OnOk, OnCanOk);
            CancelCommand = new DelegateCommand(OnCancel);
            Entity = new ApplicationUserInfo();
        }

        protected virtual void OnOk()
        {
            window.Content = null;
            window.Close();
        }

        protected virtual bool OnCanOk()
        {
            return true;
        }

        protected virtual void OnCancel()
        {
            Environment.Exit(0);
        }

        #region IUserSelector Members

        BaseWindow window = null;
        LoginUserSelector userSelectorView = new LoginUserSelector();

        public ApplicationUserInfo SelectUser()
        {
            userSelectorView.HorizontalAlignment = HorizontalAlignment.Stretch;
            userSelectorView.VerticalAlignment = VerticalAlignment.Stretch;
            userSelectorView.HorizontalContentAlignment = HorizontalAlignment.Stretch;
            userSelectorView.VerticalContentAlignment = VerticalAlignment.Stretch;


            window = new BaseWindow();
            userSelectorView.DataContext = this;
            window.DataContext = this;
            window.Style = Application.Current.FindResource("WindowStyle") as Style;
            window.Height = userSelectorView.MinHeight + 80;
            window.MinHeight = userSelectorView.MinHeight + 80;

            window.Width = userSelectorView.MinWidth + 65;
            window.MinWidth = userSelectorView.MinWidth + 65;

            window.Content = userSelectorView;

            window.ShowDialog();

            return Entity;
        }

        #endregion
    }
}
